package cn.tedu.egweibo1.controller;

import cn.tedu.egweibo1.mapper.WeiboMapper;
import cn.tedu.egweibo1.pojo.Vo.UserVo;
import cn.tedu.egweibo1.pojo.dto.WeiboDTO;
import cn.tedu.egweibo1.pojo.entity.Weibo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @Author Justin Parker
 * @Date 2024/8/9 0009 19:28
 * @Version 1.0
 */
@RestController
@RequestMapping("/v1/weibo")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;


    @RequestMapping("/insert")
    public int insertWeibo(@RequestBody WeiboDTO weiboDTO, HttpSession session) {
//        UserVo userVo = (UserVo) session.getAttribute("user");
        if (session.getAttribute("user")==null){
            return 2;
        }
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDTO,weibo);
        System.out.println("weiboDTO = " + weiboDTO + ", session = " + session);
        weibo.setUserId(((UserVo)session.getAttribute("user")).getId());
        weibo.setCreated(new Date());
        weiboMapper.insertWeibo(weibo);
        return 1;
    }
}
